package com.datastory.report.utils;

import com.datastory.report.dto.FileView;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hshe 2018/6/13.
 */
public class HtmlStrUtils {
    private static final String loadFileStr = "<a target=\"_blank\" title=\"%s\" href=\"%s?filename=%s\">";

    public static String getHtml(String fileName, String url) {
        return String.format(loadFileStr, fileName, url, fileName) + fileName + "</a></br>";
    }

    public static String getHtml(String dirName, String fileName, String url) {
        if (null == dirName || "".equalsIgnoreCase(dirName.trim())) {
            return String.format(loadFileStr, fileName, url, fileName) + fileName + "</a></br>";
        }
        return String.format(loadFileStr, fileName, url, dirName + "/" + fileName) + fileName + "</a></br>";
    }

    public static String getHtmlByFile(File file, String relatepath) {
        try {
            String path = file.getPath();
            System.out.println(path);
            File[] files = file.listFiles();
            ArrayList<FileView> fileViews = new ArrayList<>();
            for (File f : files) {
//                System.out.println(f.getName());
                FileView fileView = new FileView();
                fileView.setName(f.getName());
                fileView.setSize(f.length());
                fileView.setTime(new Date(f.lastModified()));
                fileView.setType(f.isDirectory() ? "d" : "-");
                fileViews.add(fileView);
            }

            List<FileView> collect = fileViews.stream().sorted(new Comparator<FileView>() {
                @Override
                public int compare(FileView o2, FileView o1) {
                    if (!o1.getType().equalsIgnoreCase(o2.getType())) {
                        if ("d".equalsIgnoreCase(o1.getType())) {
                            return 1;
                        } else {
                            return -1;
                        }
                    }
                    return Integer.valueOf(o1.getTime().getTime() - o2.getTime().getTime() + "");
                }
            }).collect(Collectors.toList());

            StringBuilder builder = new StringBuilder("<a href='/'>Parent Directory</a><br><hr><table>");
            for (FileView fileView : collect) {
                System.out.println(fileView.getLineData(relatepath));
                builder.append(fileView.getLineData(relatepath));
            }
            builder.append("</table>");
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

//    private String buildhtml_content(List<FileView> lists) {
//        String content = "";
//
//
//    }

    public static FileView fileViewWrapper(File f) {
        FileView fileView = new FileView();

//        if (f.isFile()) {
//            writer.write(HtmlStrUtils.getHtml(dirName, f.getName(), "download"));
//        } else {//文件夹
//            String content = HtmlStrUtils.getHtml(f.getName(), "list");
//            writer.write(content);
//        }
        return null;
    }
}
